"""
@author: xiangping
@contact: xiangpingbu@gmail.com
@time: 2020/3/3 1:49 下午
@file: batch_job
@Desc:
"""
import datetime

from app.exts import scheduler
from app.services.business import shaojie_biz_service, \
    gaolu_biz_service, \
    hanfan_biz_service, \
    lugang_biz_service, \
    lugang_qinshi_biz_service, \
    lugang_activiness_long_biz_service, lugang_activiness_short_biz_service, data_integration_service
from app.services.business.gaolu_diagnose import main
from app.services.business.hanfan import luzha_biz_service

from app.services.dal import hanfan_dao_service

############  下列参数，只有在模拟情况才需要 #######################
#

# 现有数据起始时间点，
# 即为从CG_LT_GL_GL04_ALL_shift中的开始时间点，往后数据读入模拟系统
source_time_label_gaolu = datetime.datetime.strptime("2018-01-01 00:00:00", "%Y-%m-%d %H:%M:%S")

# 模拟数据进表标注时间点
# 即为数据进入系统表 CG_LT_GL_GL04_GAOLU_INPUT， CG_LT_GL_GL04_GAOLU_PREDICT 被打上的时间点
# 因为 CG_LT_GL_GL04_GAOLU_PREDICT 表为历史数据分析提供信息，所以现阶段，不要把次时间设置到2019-01-01之前，以防数据污染
simulate_time_label_gaolu = datetime.datetime.strptime("2019-01-01 00:00:00", "%Y-%m-%d %H:%M:%S")

# 现有数据其实时间点
# 即为从 IRONMAN_ONLINE SJ_mainparametersOBT_Delay5min 中那个时间点，开始往后读取进入模拟系统
source_date_label_shaojie = datetime.datetime.strptime("2018-07-03 00:00:00", "%Y-%m-%d %H:%M:%S")

# 模拟数据进表标注时间点
simulate_time_label_shaojie = datetime.datetime.strptime("2019-01-01 00:00:00", "%Y-%m-%d %H:%M:%S")


###########################################################

def shaojie_model_predict(is_simulated=False):
    app = scheduler.app
    simulated_time_label = None
    if is_simulated:
        # 给延迟，保正模拟的输入数据已经落库
        # seconds =10  相当于 2个 SHAOJIE_INTERVAL 延迟
        simulated_time_label = simulate_time_label_shaojie - datetime.timedelta(seconds=10)
    with app.app_context():
        shaojie_biz_service.model_predict(simulated_time_label)


def gaolu_model_predict(is_simulated=False):
    app = scheduler.app
    # 非模拟状态下，为空值
    simulated_time_label = None
    if is_simulated:
        # 给延迟，保正模拟的输入数据已经落库
        # hours =2 相当于 2个 GAOLU_INTERVAL 延迟
        simulated_time_label = simulate_time_label_gaolu - datetime.timedelta(hours=2)
    with app.app_context():
        gaolu_biz_service.model_predict(simulated_time_label)


def shaojie_demo_data_stream():
    app = scheduler.app
    global source_date_label_shaojie
    global simulate_time_label_shaojie
    with app.app_context():
        shaojie_biz_service.simulate_data_stream(source_date_label_shaojie, label_time=simulate_time_label_shaojie)
    source_date_label_shaojie = source_date_label_shaojie + datetime.timedelta(seconds=5)
    simulate_time_label_shaojie = simulate_time_label_shaojie + datetime.timedelta(seconds=5)


def gaolu_demo_data_stream():
    app = scheduler.app
    global source_time_label_gaolu
    global simulate_time_label_gaolu
    with app.app_context():
        gaolu_biz_service.simulate_data_stream(source_time_label_gaolu, label_time=simulate_time_label_gaolu)
    source_time_label_gaolu = source_time_label_gaolu + datetime.timedelta(hours=1)
    simulate_time_label_gaolu = simulate_time_label_gaolu + datetime.timedelta(hours=1)


def shaojie_model_predict_online():
    app = scheduler.app
    with app.app_context():
        shaojie_biz_service.model_predict_online()


def gaolu_model_predict_online():
    app = scheduler.app
    with app.app_context():
        gaolu_biz_service.model_predict_online()


def hanfan_model_predict_online():
    app = scheduler.app
    with app.app_context():
        hanfan_biz_service.model_predict_online()


def dump_hanfan_data():
    """
    查询含钒原燃料字段数据。

    之前是通过kafka来触发，会存在kafka数据不能实时显示问题，原因: 由于含钒原燃料字段查询太多，导致执行时间过长。远大于kafka消息的频率，
    因此导致kafka消息积压，不能正常消费。

    """
    app = scheduler.app
    with app.app_context():
        hanfan_dao_service.dump_hanfan_data(start=datetime.datetime.now(), end=None)


def gaolu_banbie_backtracking_data():
    app = scheduler.app
    with app.app_context():
        gaolu_biz_service.gaolu_banbie_backtracking_data()


def dump_lugang_data():
    """
    抽样,将数据整合为分钟级,小时级,周级别数据
    :return:
    """
    app = scheduler.app
    with app.app_context():
        lugang_biz_service.dump_lugang_data()


def dump_lugang_qinshi_data():
    """
    抽样,将数据整合为分钟级,小时级,周级别数据
    :return:
    """
    app = scheduler.app
    with app.app_context():
        lugang_qinshi_biz_service.dump_lugang_qinshi_data()


def dump_lugang_qinshi_model_data():
    """
    炉缸侵蚀模型结果
    :return:
    """
    app = scheduler.app
    with app.app_context():
        lugang_qinshi_biz_service.dump_qinshi_model_data()


def dump_lugang_activeness_long():
    """
    炉缸活跃性长期数据
    :return:
    """
    app = scheduler.app
    with app.app_context():
        lugang_activiness_long_biz_service.dump_lugang_activeness_long()


def dump_lugang_activeness_predict_long():
    """
       炉缸活跃性长期预测
       :return:
       """
    app = scheduler.app
    with app.app_context():
        lugang_activiness_long_biz_service.dump_lugang_activeness_predict_long()


def dump_lugang_activeness_short():
    """
    炉缸活跃性短期数据
    :return:
    """
    app = scheduler.app
    with app.app_context():
        lugang_activiness_short_biz_service.dump_lugang_activeness_short()


def dump_lugang_activeness_predict_short():
    """
    炉缸活跃性短期预测
    :return:
    """
    app = scheduler.app
    with app.app_context():
        lugang_activiness_short_biz_service.dump_lugang_activeness_predict_short()


def cal_lugang_data_dist():
    """
    炉缸数据分布计算
    :return:
    """
    app = scheduler.app
    with app.app_context():
        end = datetime.datetime.now()
        start = end - datetime.timedelta(days=365)
        lugang_biz_service.lugang_calculate_dist(start, end, 24)


def cal_gaolu_diagnose():
    """
    高炉诊断定时任务
    :return:
    """
    app = scheduler.app
    with app.app_context():
        main.run()

def cal_luzha_hanfan_data():
    """
    高炉诊断定时任务
    :return:
    """
    app = scheduler.app
    with app.app_context():
        luzha_biz_service.dump_luzha_data()


def data_integration():
    """
    数据聚合服务
    :return:
    """
    app = scheduler.app
    with app.app_context():
        data_integration_service.dump()

def zhapi_data_dump():
    """
    渣皮数据落库
    :return:
    """
    return

